package com.tencent.qplus.paddata.stat2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.tencent.qplus.paddata.db.DataSourceManager;
import com.tencent.qplus.paddata.stat.Job;
import com.tencent.qplus.paddata.util.ConfigUtil;
import com.tencent.qplus.paddata.util.JdbcUtils;


public class ParseOnlineTimeLogJob extends Job {
	private String onlineTimeLengthFilepath = "";
	private Connection conn = null;
	
	private void praseOnlineLog2Db() throws Exception{
		File file = new File(onlineTimeLengthFilepath);
		String insertSql = "INSERT INTO uinonlinetime_? (uin,logintime,logouttime,onlinetimelength) VALUES (?,?,?,?);";
		BufferedReader br;
		
		PreparedStatement pst = null;
		try {
			
			br = new BufferedReader(new FileReader(file));
			String line = null;
			
			pst = conn.prepareStatement(insertSql);
			long i = 0;
			//420448000       2012-01-28 23:56:21     2012-01-29 00:00:21     4.00    1411
			while((line=br.readLine())!=null){
				String[] ss = line.split("\t");
				if(ss.length==5){
					int timeLength = Math.round(Float.parseFloat(ss[3]));
					if(timeLength>=60){
						long uin = Long.parseLong(ss[0]);
						pst.setInt(1, (int)(uin%100));
						pst.setString(2, ss[0]);
						pst.setString(3, ss[1]);
						pst.setString(4, ss[2]);
						pst.setString(5, ss[3]);
						pst.setInt(5,timeLength);
						pst.addBatch();
						if(++i%100==0){
							pst.executeBatch();
						}
					}
				}
			}
			pst.executeBatch();
		} catch (FileNotFoundException e) {
			logger.error(e);
			throw e;
		} catch (IOException e) {
			logger.error(e);
			throw e;
		} catch (SQLException e) {
			logger.error(e);
			throw e;
		}finally{
			JdbcUtils.release(pst);
		}
		
	}

	/**
	 * 删除数据日志表中的历史数据，保留60天
	 */
	private void deleteHistoryData() throws Exception{
		String deleteDay = dateUtils.getDateStrByDaysFromDate(statDate, -60);
		Statement st = null;
		try {
			st = conn.createStatement();
		
			for(int i=0;i<100;i++){
				String sql = "delete from uinonlinetime_"+i+" where logouttime>'"+deleteDay+"' and logouttime<'"+deleteDay+" 24'";
				try {
					st.executeUpdate(sql);
				} catch (SQLException e) {
					e.printStackTrace();
					logger.error(e);
				}
			}
		} catch (SQLException e) {
			logger.error(e);
			throw e;
		}finally{
			JdbcUtils.release(st);
		}
	}
	
	/**
	 * 重算时清楚脏数据
	 */
	private void deleteData() throws Exception{
		Statement st = null;
		try {
			st = conn.createStatement();
		
			for(int i=0;i<100;i++){
				String sql = "delete from uinonlinetime_"+i+" where logouttime>'"+this.statDate+"' and logouttime<'"+this.statDate+" 24'";
				try {
					st.executeUpdate(sql);
				} catch (SQLException e) {
					e.printStackTrace();
					logger.error(e);
				}
			}
		} catch (SQLException e) {
			logger.error(e);
			throw e;
		}finally{
			JdbcUtils.release(st);
		}
	}
	@Override
	public void run() throws Exception{
		init();
		deleteHistoryData();
		deleteData();
		praseOnlineLog2Db();
		JdbcUtils.release(conn);
	}

	@Override
	public void init() throws Exception {
		onlineTimeLengthFilepath = ConfigUtil.getPath("online_time_length_filepath")+"QQforPad_onlinelog_"+this.statDate.replace("-", "")+".txt";
		try {
			conn = DataSourceManager.getInstance().getDataSource("padversiondatadb").getConnection();
		} catch (SQLException e) {
			logger.error(e);
			throw e;
		}
	}
	
	public static void main(String[] args){
		ParseOnlineTimeLogJob job = new ParseOnlineTimeLogJob();
		job.stat(args);
	}

}
